package net.cookiefarmers.seedswap

import org.springframework.dao.DataIntegrityViolationException

class SeedController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

def	 beforeHandInterceptor = [action:this.&checkUser, except:['login']]

	def scaffold = true
	private checkUser(){ //authenitcates access
		redirect(controller:'user', action:'login')
		if(!session.user){
			redirect( action:'login')
			return false
		}
	}


    def index() { //navigates to the index of seeds page
        redirect(action: "list", params: params)
    }

    def list() { //returns the list of seeds in the index
	
	flash.message = params.max 
	//seedList = Seed.list.findByName("seed1")
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [seedInstanceList: Seed.findAll("from Seed where owner=?",[session.user.login]), seedInstanceTotal: Seed.count()] //list this farmers' seeds only
    }

    def create() { //make a new seed
        [seedInstance: new Seed(params)]
    }

    def save() { //stores the seed values 
	//params.set("owner", session.user.login)	
	params['owner'] = session.user.login	// save owner of the seed
	//flash.message = params.foo
        def seedInstance = new Seed(params)
        if (!seedInstance.save(flush: true)) {
            render(view: "create", model: [seedInstance: seedInstance])
            return
        }

		flash.message = message(code: 'default.created.message', args: [message(code: 'seed.label', default: 'Seed'), seedInstance.id])
//flash.message = params.foo
        redirect(action: "show", id: seedInstance.id)
	
    }

    def show() { //displays the seed
        def seedInstance = Seed.get(params.id)
        if (!seedInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'seed.label', default: 'Seed'), params.id])
            redirect(action: "list")
            return
        }

        [seedInstance: seedInstance]
    }

    def edit() { //allows value fields to be changed
        def seedInstance = Seed.get(params.id)
        if (!seedInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'seed.label', default: 'Seed'), params.id])
            redirect(action: "list")
            return
        }

        [seedInstance: seedInstance]
    }

    def update() { //saves the changed values from edit, overwriting the old values
        def seedInstance = Seed.get(params.id)
        if (!seedInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'seed.label', default: 'Seed'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (seedInstance.version > version) {
                seedInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'seed.label', default: 'Seed')] as Object[],
                          "Another user has updated this Seed while you were editing")
                render(view: "edit", model: [seedInstance: seedInstance])
                return
            }
        }

        seedInstance.properties = params

        if (!seedInstance.save(flush: true)) {
            render(view: "edit", model: [seedInstance: seedInstance])
            return
        }

		flash.message = message(code: 'default.updated.message', args: [message(code: 'seed.label', default: 'Seed'), seedInstance.id])
        redirect(action: "show", id: seedInstance.id)
    }

    def delete() { //allows the seed to be deleted if it already existed
        def seedInstance = Seed.get(params.id)
        if (!seedInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'seed.label', default: 'Seed'), params.id])
            redirect(action: "list")
            return
        }

        try {
            seedInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'seed.label', default: 'Seed'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'seed.label', default: 'Seed'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
}
